﻿package com.toncen.samepms.system.dao;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.toncen.samepms.basic.dao.UserDao;
import com.toncen.samepms.basic.domain.User;
import com.toncen.samepms.common.AbstractDao;
import com.toncen.samepms.common.ComitConstant;
import com.toncen.samepms.system.domain.Message;
import com.sky.common.json.JSONUtils;

/**
 * 描述：系统消息表SYSTEM_MESSAGE的dao文件
 * 
 * @author zq
 */
public class MessageDao extends AbstractDao<Message> {

	public MessageDao() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.comit.common.db.BaseDao#initialize()
	 */
	@Override
	protected void initialize() {
		setStatementPrefix("Message");
		setStatementDivision(".");
	}

	@Override
	public long save(Message obj) throws Exception {
		// TODO Auto-generated method stub
		long id = super.save(obj);
		long pkId = obj.getId();
		String reciver = obj.getReceiver();
		String[] recives = reciver.split(",", -1);
		Message message = null;
		for (String rec : recives) {
			message = obj;
			message.setId(-1);
			message.setSysId(pkId);
			message.setType(1);
			message.setReceiver(rec);
			super.save(message);
		}
		return id;
	}

	/**
	 * 
	 * TODO 发送单条系统消息，由系统管理员发出
	 * 
	 * @author Sep 24, 2013 10:00:31 AM ZQ
	 * @param target
	 *            消息接收人
	 * @param subject
	 *            标题
	 * @param content
	 *            内容
	 * @return
	 * @throws Exception
	 * 
	 */
	public boolean saveDefaultAdvice(User target, String subject, String content) throws Exception {
		Message message = setBaseMessageInfo(subject, content);
		setDefaultSender(target, message);

		boolean success = saveAdvice(message);
		return success;
	}

	/**
	 * 给多个人发送消息 TODO
	 * 
	 * @author Sep 24, 2013 10:36:00 AM ZQ
	 * @param fromUser
	 * @param toUsers
	 * @param subject
	 * @param content
	 * @return
	 * @throws Exception
	 * 
	 */
	public boolean saveAdvice(User fromUser, String toUsers, String subject, String content) throws Exception {
		Message message = setBaseMessageInfo(subject, content);
		// 发送人
		message.setSender(fromUser);
		// 接收人
		message.setReceiver(toUsers);

		boolean success = saveAdvice(message);
		return success;
	}

	private boolean saveAdvice(Message message) throws Exception {
		long messageId = save(message);
		boolean success = false;
		if (messageId > 0) {
			success = true;
		}
		return success;
	}

	/**
	 * 
	 * TODO 设置系统消息的基本信息
	 * 
	 * @author Sep 24, 2013 10:29:33 AM ZQ
	 * @param subject
	 * @param content
	 * @return
	 * 
	 */
	private Message setBaseMessageInfo(String subject, String content) {
		Message message = new Message();
		// 标题
		message.setTitle(subject);
		// 内容
		message.setContent(content);
		// 发送时间
		Date sendDate = new Date();
		message.setSendTime(sendDate);
		// 开始时间
		message.setStartTime(sendDate);
		// 结束时间
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(sendDate);
		calendar.add(Calendar.DAY_OF_MONTH, 7);
		// 结束时间默认为开始时间+7天
		message.setEndTime(calendar.getTime());
		message.setType(0);
		return message;
	}

	/**
	 * TODO 设置默认发送人
	 * 
	 * @author Sep 24, 2013 10:27:38 AM ZQ
	 * @param target
	 * @param message
	 * @throws Exception
	 * 
	 */
	private void setDefaultSender(User target, Message message) throws Exception {
		// 发送人，配在系统配置
		ConfigurationDao configDao = new ConfigurationDao();
		String userId = configDao.getValueByName(ComitConstant.DEFAULT_ADMIN_ID);
		User user = (User) new UserDao().find(Long.parseLong(userId));
		if (user == null) {
			user = new User();
		}
		message.setSender(user);
		// 接收人
		message.setReceiver(String.valueOf(target.getId()));
	}

	public static void main(String[] args) throws Exception {
		MessageDao dao = new MessageDao();
		// Message message=new Message();
		//		
		// UserDao ud=new UserDao();
		// User user=ud.load(1331626056324l);
		//			
		// message.setSender(user);
		// message.setReceiver("1,2,3");
		// message.setType(0);
		// message.setContent("321");
		//		
		// dao.save(message);

		// dao.load(1332313131031l);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", "1331626056324");
		map.put("row", "top 10");
		System.out.println(JSONUtils.object2JSON(dao.getNewMessage(map)));
	}

	/**
	 * 
	 * TODO 取当前用户最新消息
	 * 
	 * @author Mar 21, 2012 5:37:20 PM ZQ
	 * @param parMap
	 * @return
	 * @throws Exception
	 * 
	 */
	public List<Message> getNewMessage(Map<String, Object> parMap) throws Exception {
		List<Message> messageList = getSqlMap().queryForList(getStatement("getNewMessage"), parMap);
		return messageList;
	}

	/**
	 * 根据用户获取消息给android端 TODO
	 * 
	 * @author Aug 14, 2013 11:39:50 AM ZQ
	 * @param parMap
	 * @return
	 * @throws Exception
	 * 
	 */
	public List<Message> getNewMessageForAndroid(Map<String, Object> parMap) throws Exception {
		List<Message> messageList = getSqlMap().queryForList(getStatement("getNewMessage"), parMap);
		for (Message msg : messageList) {
			msg.setHasRead(1);
			update(msg);
		}
		return messageList;
	}

}
